<?php 
namespace xfyzf\api\controller;

class WXWindows_Protocol{
    public static function XFApi_Url(){
        return "http://pc.wfkc.tech:699";
    }
    public static function wxlogin(){
        $url = self::XFApi_Url()."/api/Client/WXCreate";
       
        $Header =  array('Content-Type: application/json-patch+json');
        $res = json_decode(xfhttp_post_yun($url,$Header,$post), true);
        $qrcode=self::XFApi_Url()."/api/Login/WXGetLoginQrcode";
        
        
        $guid1=$res['data']['Guid'];
        
         $data = array(
                  'Guid'=>$guid1
                  );
        $resq=json_decode(xfhttp_post_yun($qrcode,$Header,$data),true);
        
        
        if ($resq["data"]["qrcode"]){
            	$result = ["uuid" => $resq["data"]["uuid"], "guid" => strval($guid1), "qr_url" => $resq["data"]["qrcode"]];
        }else{
            return false;
        }
        return $result;
    }
    public static function wxCheckLoginQrcode($guid, $uuid){
        $Header =  array('Content-Type: application/json-patch+json');
        $WXCheck = self::XFApi_Url() . "/api/Login/WXCheckLoginQrcode";
        $post = array(
    	    'Guid'=>$guid,
    	    'Uuid'=>$uuid
    	    );
        $res = json_decode(xfhttp_post_yun($WXCheck, $Header,$post), true);
        if ($res["data"]["state"] == 0) {
    		$result = ["code" => 0, "msg" => "等待扫码中", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 1) {
    		$result = ["code" => 2, "msg" => "等待确认中", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 4) {
    		$result = ["code" => -1, "msg" => "系统错误,请联系管理员", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 2) {
    		$WXLogin = self::XFApi_Url() . "/api/Login/WXLoginManual";
    		$pass = $res["data"]["wxnewpass"];
    		$wxid = $res["data"]["wxid"];
    		$post = array(
    		    'Guid'=>$guid,
    		    'Channel'=>1,
    		    'UserName'=>$wxid,
    		    'Password'=>$pass
    		    );
    		$res2 = json_decode(xfhttp_post_yun($WXLogin,$Header,$post), true);
    		$result = ["code" => 200, "msg" => "登录成功", "guid" => $guid];
    	
    	}
    	return $result;
    }
    public static function WXHeartBeat($guid){
        $Header =  array('Content-Type: application/json-patch+json');
        $url= self::XFApi_Url(). "/api/Heartbeat/WXHeartBeat";
        $post = array(
    	    'Guid'=>$guid
    	  );
    	$res = json_decode(xfhttp_post_yun($url, $Header,$post), true); 
    	if($res['data']){
    	 	   $result = ["code" => 1, "msg" => "心跳正常"];
	 	}else{
	 	     $result = ["code" => -1, "msg" => "账户已经离线"];
	 	}
	 	return $result;
    }
    public static function gmqrurl($guid){
        $WXJSlogin = self::XFApi_Url() . "/api/Extension/WXTenPay";
        $Header = array('Content-Type: application/json-patch+json');
        $postData = array(
            'Guid' => $guid,
            'Type' => 2,
            'ReqText' => '1',
            'ReqTextWx' => '1'
        );
        $res = json_decode(xfhttp_post_yun($WXJSlogin,$Header,$postData), true);
       
        if ($res['flag'] === 0 && isset($res['data']['reqText']['buffer'])){
            $buffer = json_decode($res['data']['reqText']['buffer'], true);
            if (isset($buffer['user_info']['transfer_url'])){
                $transferUrl = $buffer['user_info']['transfer_url'];
                $Url = stripslashes($transferUrl);
                return $Url;
            }
        }
        return false;
    }
    public static function Order($guid){
        $WXSyncMsg = self::XFApi_Url()."/api/Message/WXSyncMsg";
        $Header =  array('Content-Type: application/json-patch+json');
        $post  = array(
            "Guid"=>$guid
            );
         $results= xfhttp_post_yun($WXSyncMsg,$Header,$post);
         $result = self::stripslashes_deep($results);
         return $result;
    }
    public static function stripslashes_deep ( $value ){
        $value = is_array ( $value ) ?
        array_map ( 'stripslashes_deep' , $value ) :
        stripslashes ( $value );
        return $value ;
    }
    
    public static function Xwcode($guid, $appid = "wx264e9b6d4d484f51"){
        $WXJSlogin = self::XFApi_Url() . "/api/Auth/WXJSLogin";
        $Header =  array('Content-Type: application/json-patch+json');
        $post = array("Guid"=>$guid,"AppId"=>$appid);
        $res = json_decode(xfhttp_post_yun($WXJSlogin, $Header ,$post),true);
        $code=$res['data']['code'];
        $url = "https://payapp.wechatpay.cn/receiptwxmgr/account/list?miniprogram_version=3.11.12&js_code=".$code;
        $res=json_decode(get_curl($url),true);
        $sid=$res['sid'];
        $filteredData = [];
        $pattern = '/^[a-zA-Z0-9]{28}$/';
        foreach ($res['data']['account_list'] as $account) {
            if (preg_match($pattern, $account['account_id'])){
                $filteredData['account_id'] = $account['account_id'];
                break;
            }
        }
        if($filteredData['account_id']){
            return ['code'=>1,'sid'=>$sid,'account_id'=>$filteredData['account_id']];
        }else{
            return ['code'=>-1];
        }
    }
    public static function WXID($sid,$fee,$remark,$account_id){
        $fee = $fee*100;
        $url = 'https://payapp.wechatpay.cn/receiptwxmgr/receipt/create?miniprogram_version=3.11.12&fee='.$fee.'&remark='.$remark.'&remark_pic_urls=&option_list=%5B%5D&account_id='.$account_id.'&account_type=2&sid='.$sid;
       
         $res=json_decode(get_curl($url),true);
         if($res['data']['receipt']['receipt_id']){
              return ['code'=>1,'receipt_id'=>$res['data']['receipt']['receipt_id']];
         }else{
             return ['code'=>-1,'msg'=>$res['msg']];
         }
    }
    public static function WXskdqrcode($sid,$account_id,$receipt_id){
        $url = 'https://payapp.wechatpay.cn/receiptwxmgr/receipt/getwxacode?miniprogram_version=3.11.12&wxacode_path_type=1&receipt_id='.$receipt_id.'&account_id='.$account_id.'&account_type=2&sid='.$sid;
        $res=json_decode(get_curl($url),true);
        if($res['data']['qrcode']){
            $result=array("code"=>1,"msg"=>"获取成功","qrcode"=>$res['data']['qrcode']);
        }else{
            $result=array("code"=>-1,"msg"=>"获取失败");
        }
        return $result;
    }
}

?>